<template>
  <div class="app-container">
    <div
      class="filter-container"
      style="margin-bottom: 0px;padding-bottom: 0px"
    >
      <el-row>
        <!-- 校区 -->
        <el-select
          v-model="listQuery.orgId"
          placeholder="校区"
          clearable
          style="width: 180px"
          class="filter-item1"
          @change="handleOrgIdChange"
        >
          <el-option
            v-for="option in orgIdOptions"
            :key="option.key"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
        <!-- 姓名-->
        <el-input
          placeholder="姓名"
          v-model="listQuery.customerName"
          style="width: 200px;"
          @keyup.enter.native="handleFilter"
          clearable
        />

        <!--电话 -->
        <el-input
          placeholder="电话"
          v-model="listQuery.customerPhone"
          style="width: 200px;"
          @keyup.enter.native="handleFilter"
          clearable
        />
        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
          >{{ $t("table.search") }}</el-button
        >
        <el-button
          v-waves
          :loading="downloadLoading"
          v-permission="['business:export']"
          class="filter-item"
          type="primary"
          icon="el-icon-download"
          @click="handleDownload"
          >{{ $t("table.export") }}</el-button
        >
        <el-button type="text" @click="toggleMoreFilters()">
          {{ this.toggleBtnText }}
          <i v-if="this.toggleBtnText !== '展开' "
            class="el-select__caret el-input__icon el-icon-arrow-up is-reverse"
          ></i>

          <i v-else
            class="el-select__caret el-input__icon el-icon-arrow-down is-reverse"
          ></i>
        </el-button>
      </el-row>

      <el-row :style="displayMoreFilters" class="pd-10">
        <!-- 渠道类型 -->
        <el-select
          v-model="listQuery.channelTypeId"
          placeholder="渠道类型"
          clearable
          style="width: 180px"
          class="filter-item1"
          @change="handleChannelTypeIdChange"
        >
          <el-option
            v-for="option in channelTypeIdOptions"
            :key="option.key"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
        <!-- 渠道 -->
        <el-select
          v-model="listQuery.channelId"
          placeholder="渠道"
          clearable
          style="width: 180px"
          class="filter-item1"
        >
          <el-option
            v-for="option in channelIdOptions"
            :key="option.key"
            :label="option.label"
            :value="option.value"
          />
        </el-select>

        <!-- 科系 -->
        <el-select
          v-model="listQuery.departmentId"
          placeholder="科系"
          clearable
          style="width: 180px"
          class="filter-item1"
          @change="handleDepartmentIdChange"
        >
          <el-option
            v-for="option in departmentIdOptions"
            :key="option.key"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
        <!-- 课   程 -->
        <el-select
          v-model="listQuery.courseId"
          placeholder="课程"
          clearable
          style="width: 180px"
          class="filter-item1"
        >
          <el-option
            v-for="option in courseOptions"
            :key="option.key"
            :label="option.label"
            :value="option.value"
          />
        </el-select>

        <!-- 咨询师 -->
        <el-select
          v-model="listQuery.ownerId"
          placeholder="咨询师"
          clearable
          style="width: 180px"
          class="filter-item1"
        >
          <el-option
            v-for="option in ownerIdOptions"
            :key="option.key"
            :label="option.label"
            :value="option.value"
          />
        </el-select>

        <!-- 跟进次数 -->
        <el-input
          placeholder="跟进次数"
          v-model="listQuery.updateCount"
          style="width: 200px;"
          @keyup.enter.native="handleFilter"
          clearable
        />
      </el-row>

      <el-row class="pd-10" :style="displayMoreFilters">
        <el-select
          v-model="listQuery.isAvailable"
          placeholder="线索状态"
          clearable
          style="width: 180px"
          class="filter-item1"
        >
          <el-option
            v-for="option in isAvailableOptions"
            :key="option.key"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
        <el-select
          v-model="listQuery.clueStatusCode"
          placeholder="线索分类"
          clearable
          style="width: 180px"
          class="filter-item1"
        >
          <el-option
            v-for="option in clueStatusCodeOptions"
            :key="option.key"
            :label="option.label"
            :value="option.value"
          />
        </el-select>

        <!-- 咨询 日期 -->
        <el-date-picker
          v-model="createTime"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          type="daterange"
          @change="handleCreateTimeChange"
          start-placeholder="咨询开始日期"
          end-placeholder="咨询结束日期"
          range-separator="至"
          style="width: 400px"
        ></el-date-picker>
        <!-- 报名日期-->
        <el-date-picker
          v-model="enrollTime"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          type="daterange"
          @change="handlePromiseVisitDateChange"
          start-placeholder="报名开始时间"
          end-placeholder="报名结束时间"
          range-separator="至"
          style="width: 400px"
        ></el-date-picker>
      </el-row>

      <el-row class="pd-10" :style="displayMoreFilters">
        <!-- 是否承诺到访-->
        <el-select
          v-model="listQuery.isPromiseVisit"
          placeholder="是否承诺到访"
          clearable
          style="width: 180px"
          class="filter-item1"
        >
          <el-option
            v-for="option in isPromiseVisitOptions"
            :key="option.key"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
        <!-- 诺到访日期-->
        <el-date-picker
          v-if="listQuery.isPromiseVisit == 1"
          v-model="promiseVisitDate"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          type="daterange"
          @change="handlePromiseVisitDateChange"
          start-placeholder="诺到访开始时间"
          end-placeholder="诺到访结束时间"
          range-separator="至"
          style="width: 400px"
        ></el-date-picker>
        <!-- 是否到访-->
        <el-select
          v-model="listQuery.isVisit"
          placeholder="是否到访"
          clearable
          style="width: 180px"
          class="filter-item1"
        >
          <el-option
            v-for="option in isVisitOptions"
            :key="option.key"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
        <!-- 到访日期-->
        <el-date-picker
          v-if="listQuery.isVisit == 1"
          v-model="visitTime"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          type="daterange"
          @change="handleVisitTimeChange"
          start-placeholder="到访开始日期"
          end-placeholder="到访结束日期"
          range-separator="至"
          style="width: 400px;"
        ></el-date-picker>
      </el-row>
    </div>

    <el-row>
      <el-col style="margin-bottom: 0px">
        <div style="vertical-align: bottom;float: left;padding: 10px 0px;font-size: 16px;font-weight: normal;color: #303133;"> <label>已报名线索列表</label></div>

        <el-popover
          ref="popover4"
          placement="left"
          width="900px"
          trigger="click"
        >
          <el-checkbox-group v-model="checkboxVal" style="width: 700px">
            <el-row style="margin-bottom: 10px">
              <el-col :span="6">
                <el-checkbox disabled label="customerName">客户姓名</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox disabled label="customerPhone">客户电话</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox disabled label="clueStatusText">线索分类</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox disabled label="channelInfo">渠道</el-checkbox>
              </el-col>
            </el-row>

            <el-row style="margin-bottom: 10px">
              <el-col :span="6" >
                <el-checkbox disabled label="orgName">校区Id</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox disabled label="createTime">咨询日期</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox disabled label="ownerName">跟进人</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox disabled label="updateCount">跟进次数</el-checkbox>
              </el-col>
            </el-row>
            <el-row style="margin-bottom: 10px">
              <el-col :span="6">
                <el-checkbox
                  label="consultCourseNames"
                >咨询课程</el-checkbox
                ></el-col
              >
              <el-col :span="6">
                <el-checkbox style="width: 80px" label="auditonCourseNames"
                >试听课程</el-checkbox
                ></el-col
              >
              <el-col :span="6">
                <el-checkbox style="width: 80px" label="enrollCourseNames"
                >报名课程</el-checkbox
                ></el-col
              >
              <el-col :span="6">
                <el-checkbox style="width: 80px" label="creatorName"
                >录入人</el-checkbox
                ></el-col
              >

            </el-row>
            <el-row style="margin-bottom: 10px">
              <el-col :span="6">
                <el-checkbox label="customerGenderName">客户性别</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox label="customerAge">客户年龄</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox label="customerRemark">备注</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox label="isPromiseVisitText"
                >是否承诺到访</el-checkbox
                >
              </el-col>
            </el-row>

            <el-row style="margin-bottom: 10px">
              <el-col :span="6">
                <el-checkbox label="promiseVisitDate"
                >承诺到访时间</el-checkbox
                >
              </el-col>
              <el-col :span="6">
                <el-checkbox label="isSureVisitText">确定到访状态</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox label="sureVisitDate">确定到访时间</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox label="isVisitText">是否到访</el-checkbox>
              </el-col>

            </el-row>

            <el-row style="margin-bottom: 10px">
              <el-col :span="6">
                <el-checkbox label="visitTime">到访时间</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox label="noVisitReasonText">未到访原因</el-checkbox>
              </el-col>
            <el-col :span="6">
              <el-checkbox label="isAuditionText">是否试听</el-checkbox>
            </el-col>
            <el-col :span="6">
              <el-checkbox label="isEnrollText">是否报名</el-checkbox>
            </el-col>

            </el-row>
            <el-row style="margin-bottom: 10px">
              <el-col :span="6">
                <el-checkbox label="noEnrollReasonText"
                >未报名原因</el-checkbox
                >
              </el-col>
              <el-col :span="6">
                <el-checkbox label="noEnrollReasonRemark"
                >未报名原因补充</el-checkbox
                >
              </el-col>
              <el-col :span="6">
                <el-checkbox label="nextFollowTime">下次跟进时间</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox label="isTopText">是否置顶</el-checkbox>
              </el-col>
            </el-row>
            <el-row style="margin-bottom: 10px">
              <el-col :span="6">
                <el-checkbox label="operatorName">修改人</el-checkbox>
              </el-col>
              <el-col :span="6">
                <el-checkbox label="updateTime">最后操作时间</el-checkbox>
              </el-col>
            </el-row>
          </el-checkbox-group>
        </el-popover>
        <el-button v-popover:popover4 type="text" style="float:right"
          >选择列</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column

        v-for="fruit in formThead"
        :key="fruit"
        :label="fruit | labelFilter"
      >
        <template slot-scope="scope">
          <div v-if="fruit === 'customerName'">
            <img v-if="scope.row['isTop'] ==='1'" :src="topImg" style="width:50px;position: absolute;top:0px;right:0px;">
            {{ scope.row[fruit] }}

          </div>
          <div v-else-if="fruit === 'consultCourseNames'">
            <el-popover trigger="hover" placement="bottom">
              <el-table :data="scope.row.clueCourList" style="width: 100%">
                <el-table-column
                  prop="courseName"
                  label="课程名称"
                  width="180"
                ></el-table-column>
              </el-table>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium" type="danger"
                  >{{ scope.row.clueCourList.length }}门</el-tag
                >
              </div>
            </el-popover>
          </div>
          <div v-else-if="fruit === 'auditonCourseNames'">
            <el-popover trigger="hover" placement="bottom">
              <el-table :data="scope.row.auditionList" style="width: 100%">
                <el-table-column
                  prop="courseName"
                  label="试听课程"
                  width="180"
                ></el-table-column>
                <el-table-column
                  prop="teacherName"
                  label="老师"
                  width="180"
                ></el-table-column>
                <el-table-column
                  prop="courseTime"
                  label="试听时间"
                  width="180"
                ></el-table-column>


              </el-table>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium" type="warning"
                  >{{ scope.row.auditionList.length }}门</el-tag
                >
              </div>
            </el-popover>
          </div>
          <div v-else-if="fruit === 'updateCount'">
            <el-popover trigger="hover" placement="bottom">
              <el-table :data="scope.row.updateRecordList" style="width: 100%">
                <el-table-column
                  prop="creatorName"
                  label="跟进人"
                  width="180"
                ></el-table-column>
                <el-table-column
                  :show-overflow-tooltip="true"
                  prop="updateInfo"
                  label="跟进信息"
                  width="180"
                ></el-table-column>
                <el-table-column
                  prop="createTime"
                  label="跟进时间"
                  width="180"
                ></el-table-column>
              </el-table>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium" type="success"
                  >{{ scope.row.updateRecordList.length }}次</el-tag
                >
              </div>
            </el-popover>
          </div>
          <div v-else-if="fruit === 'enrollCourseNames'">
            <el-popover trigger="hover" placement="bottom">
              <el-table :data="scope.row.enrollCourseList" style="width: 100%">
                <el-table-column
                  prop="courseName"
                  label="报名课程"
                  width="180"
                ></el-table-column>
                <el-table-column
                  prop="enrolPrice"
                  label="报名费"
                  width="180"
                ></el-table-column>
                <el-table-column
                  prop="enrollTime"
                  label="报名时间"
                  width="180"
                ></el-table-column>
                <el-table-column
                  prop="materialCost"
                  label="材料费"
                  width="180"
                ></el-table-column>
                <el-table-column
                  prop="courseTime"
                  label="课时"
                  width="180"
                ></el-table-column>
                <el-table-column
                  prop="extraCourseTime"
                  label="赠送课时"
                  width="180"
                ></el-table-column>
              </el-table>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium"
                  >{{ scope.row.enrollCourseList.length }}门</el-tag
                >
              </div>
            </el-popover>
          </div>

          <div v-else>
            {{ scope.row[fruit] }}
          </div>
        </template>
      </el-table-column>

      <el-table-column
			v-permission="['business:clue:update','business:clue:delete']"
        :label="$t('table.actions')"
        align="center"
        fixed="right"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="['business:clue:update']"
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="toEdit(scope.row.clueId)"
          >编辑</el-button
          >
          <el-button
            v-permission="['business:clue:delete']"
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
          >{{ $t("table.delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  fetchList, //渠道信息
  fetchOrg,
  fetchChannel,
  fetchCourse,
  fetchTeahcer,
  fetchOwner,
  createClue,
  fetchExportList,
  deleteClue

  //课程信息
  //老师信息
  //咨询师
} from "@/api/clue";
import waves from "@/directive/waves"; // 水波纹指令
import { parseTime } from "@/utils";
import Data from "@/api/pcaa";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import topImg from '@/assets/clue/top.jpg'
import permission from "@/directive/permission/index.js"; // 权限判断指令

const defaultFormThead = [
  "customerName",
  "customerPhone",
  "clueStatusText",
  "channelInfo",

  "orgName",
  "createTime",
  "ownerName",
  "updateCount"
];

const labelMap = {
  consultCourseNames: "咨询课程",
  auditonCourseNames: "试听课程",
  updateCount: "跟进次数",
  enrollCourseNames: "报名课程",
  creatorName: "录入人",
  orgName: "校区",
  createTime: "咨询日期",
  channelInfo: "渠道",
  customerName: "客户姓名",
  customerAge: "客户年龄",
  customerGenderName: "客户性别",
  customerPhone: "客户电话",
  customerRemark: "备注",
  isPromiseVisitText: "是否承诺到访",
  promiseVisitDate: "承诺到访时间",
  isSureVisitText: "确定到访",
  sureVisitDate: "确定到访时间",
  isVisitText: "是否到访",
  visitTime: "到访时间",
  noVisitReasonText: "未到访原因",
  isAuditionText: "是否试听",
  isEnrollText: "是否报名",
  noEnrollReasonText: "未报名原因",
  noEnrollReasonRemark: "未报名原因补充",
  clueStatusText: "线索分类",
  nextFollowTime: "下次跟进时间",
  isTopText: "是否置顶",
  ownerName: "跟进人",
  lastFollowTime: "最后跟进时间",
  operatorName: "修改人",
  updateTime: "最后操作时间"
}

export default {
  name: "enrolledClueTable",
  components: { Pagination },
  directives: {
    waves,
    permission
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: "success",
        2: "info",
        0: "danger"
      };
      return statusMap[status];
    },
    statusNameFilter(status) {
      const statusNameMap = {
        1: "启用",
        2: "未激活",
        0: "禁用"
      };
      return statusNameMap[status];
    },
    sexNameFilter(sex) {
      const sexNameMap = {
        1: "男",
        2: "保密",
        0: "女"
      };
      return sexNameMap[sex];
    },
    labelFilter(labelKey) {
      return labelMap[labelKey]
    }
  },
  data() {
    return {
      topImg: topImg,
      downloadLoading: false,
      channelTypeIdOptions: [], //渠道类型
      channelIdOptions: [], //渠道
      orgIdOptions: [], //校区
      departmentIdOptions: [], //科系
      courseOptions: [], //课程
      ownerIdOptions: [], //咨询师
      isAvailableOptions: [
        {
          value: "1",
          label: "有效"
        },
        {
          value: "0",
          label: "无效"
        }
      ],
      clueStatusCodeOptions: [
        {
          value: "1",
          label: "有意向"
        },
        {
          value: "2",
          label: "有需求"
        },
        {
          value: "3",
          label: "有望成交"
        },
        {
          value: "4",
          label: "无法联系"
        },
        {
          value: "5",
          label: "无需求"
        },
        {
          value: "6",
          label: "号码错误  "
        }
      ], //线索 分类
      isPromiseVisitOptions: [
        {
          value: "1",
          label: "承诺到访"
        },
        {
          value: "0",
          label: "未承诺到访"
        }
      ],
      // 是否承诺到访
      isVisitOptions: [
        {
          value: "1",
          label: "到访"
        },
        {
          value: "0",
          label: "未到访"
        }
      ], //是否到访
      createTime: null,
      promiseVisitDate: null,
      visitTime: null,
      enrollTime: null,

      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        size: 20,
        //渠道类型
        channelTypeId: "",
        //渠道
        channelId: "",
        //校区
        orgId: "",
        //科系
        departmentId: "",
        //课程
        courseId: "",
        //咨询师
        ownerId: "",

        isEnroll: '1',

        isAvailable: "",
        //线索分类
        clueStatusCode: "",
        //咨询日期

        createTimeStart: "",
        createTimeEnd: "",

        //是否承诺到访
        isPromiseVisit: "",
        //诺到访日期范围

        promiseVisitDateStart: "",
        promiseVisitDateEnd: "",



        //是否到访
        isVisit: "",
        //到访日期

        visitTimeStart: "",
        visitTimeEnd: "",


        enrollCourseTimeStart:'',
        enrollCourseTimeEnd:'',

        //姓名
        customerName: "",
        //电话
        customerPhone: "",
        //跟进次数
        updateCount: ""
      },

      //是否展示更多的过滤条件
      displayMoreFilters: "display:none",
      toggleBtnText: "展开",

      checkboxVal: defaultFormThead, // checkboxVal
      formThead: [
        "customerName",
        "customerPhone",
        "clueStatusText",
        "channelInfo",

        "orgName",
        "createTime",
        "ownerName",
        "updateCount",

        "consultCourseNames",
        "auditonCourseNames",
        "enrollCourseNames",
        "creatorName",

        "customerAge",
        "customerGenderName",
        "customerRemark",
        "isPromiseVisitText",
        "promiseVisitDate",
        "isSureVisitText",
        "sureVisitDate",
        "isVisitText",
        "visitTime",
        "noVisitReasonText",
        "isAuditionText",
        "isEnrollText",
        "noEnrollReasonText",
        "noEnrollReasonRemark",
        "nextFollowTime",
        "operatorName",
        "updateTime"
      ],
      formTheadOptions: [
        "customerName",
        "customerPhone",
        "clueStatusText",
        "channelInfo",

        "orgName",
        "createTime",
        "ownerName",
        "updateCount",

        "consultCourseNames",
        "auditonCourseNames",
        "enrollCourseNames",
        "creatorName",

        "customerAge",
        "customerGenderName",
        "customerRemark",
        "isPromiseVisitText",
        "promiseVisitDate",
        "isSureVisitText",
        "sureVisitDate",
        "isVisitText",
        "visitTime",
        "noVisitReasonText",
        "isAuditionText",
        "isEnrollText",
        "noEnrollReasonText",
        "noEnrollReasonRemark",
        "nextFollowTime",
        "operatorName",
        "updateTime"
      ]
    };
  },
  created() {
    const arr = JSON.parse(localStorage.getItem("enrolledTableHead"));
    if (arr) {
      this.checkboxVal = arr;
      this.formThead = arr;
    } else {
      this.checkboxVal = defaultFormThead;
      this.formThead = defaultFormThead;
    }
    this.getList();
    this.getOrgIdOptions();
  },
  watch: {
    "listQuery.isAvailable": {
      handler(newIsAvailable, oldIsAvailable) {
        this.listQuery.clueStatusCode = "";
        if (newIsAvailable == "1") {
          this.clueStatusCodeOptions = [
            { value: "1", label: "有需求" },
            { value: "2", label: "有意向" },
            { value: "3", label: "有望成交" }
          ];
        } else if (newIsAvailable == "0") {
          this.clueStatusCodeOptions = [
            { value: "4", label: "无法联系" },
            { value: "5", label: "无需求" },
            { value: "6", label: "号码错误" }
          ];
        } else {
          this.clueStatusCodeOptions = [];
        }
      },
      deep: true,
      immediate: true
    },
    "listQuery.isPromiseVisit": {
      handler(newIsAudition, oldIsAudition) {
        if (newIsAudition != 1) {
          this.promiseVisitDate = null;
          this.listQuery.promiseVisitDateStart = "";
          this.listQuery.promiseVisitDateEnd = "";
        }
      },
      deep: true,
      immediate: true
    },
    createTime: {
      handler(newIsAudition, oldIsAudition) {
        if (newIsAudition) {
          this.listQuery.createTimeStart = newIsAudition[0];
          this.listQuery.createTimeEnd = newIsAudition[1];
        } else {
          this.listQuery.createTimeStart = "";
          this.listQuery.createTimeEnd = "";
        }
      },
      deep: true,
      immediate: true
    },
    enrollTime: {
      handler(newIsAudition, oldIsAudition) {
        if (newIsAudition) {
          this.listQuery.enrollCourseTimeStart= newIsAudition[0];
          this.listQuery.enrollCourseTimeEnd = newIsAudition[1];
        } else {
          this.listQuery.enrollCourseTimeStart = "";
          this.listQuery.enrollCourseTimeEnd = "";
        }
      },
      deep: true,
      immediate: true
    },

    promiseVisitDate: {
      handler(newIsAudition, oldIsAudition) {
        if (newIsAudition) {
          this.listQuery.promiseVisitDateStart = newIsAudition[0];
          this.listQuery.promiseVisitDateEnd = newIsAudition[1];
        } else {
          this.listQuery.promiseVisitDateStart = "";
          this.listQuery.promiseVisitDateEnd = "";
        }
      },
      deep: true,
      immediate: true
    },
    visitTime: {
      handler(newIsAudition, oldIsAudition) {
        if (newIsAudition) {
          this.listQuery.visitTimeStart = newIsAudition[0];
          this.listQuery.visitTimeEnd = newIsAudition[1];
        } else {
          this.listQuery.visitTimeStart = "";
          this.listQuery.visitTimeEnd = "";
        }
      },
      deep: true,
      immediate: true
    },
    "listQuery.isVisit": {
      handler(newIsAudition, oldIsAudition) {
        if (newIsAudition != 1) {
          this.visitTime = null;
          this.listQuery.visitTimeStart = "";
          this.listQuery.visitTimeEnd = "";
        }
        // if (newIsAudition != 1) {
        //   this.promiseVisitDate = null;
        //   listQuery.promiseVisitDateStart = "";
        //   listQuery.promiseVisitDateEnd = "";
        // }
      },
      deep: true,
      immediate: true
    },
    checkboxVal(valArr) {
      this.formThead = this.formTheadOptions.filter(
        i => valArr.indexOf(i) >= 0
      );
      this.key = this.key + 1; // 为了保证table 每次都会重渲 In order to ensure the table will be re-rendered each time
      localStorage.setItem("enrolledTableHead", JSON.stringify(valArr));
    }
  },
  methods: {
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.list = response.data;
        this.total = response.count;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.current = 1;
      this.getList();
    },
    handleDelete(row) {
      this.$confirm(
        "此操作将永久删除该条线索","是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          this.listLoading = true;
          deleteClue(row.clueId).then(() => {
            this.listLoading = false;
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            const index = this.list.indexOf(row);
            this.list.splice(index, 1);
          }).catch(err => {
            this.listLoading = false
          })
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // handleModifyStatus(row, status) {
    //   this.listLoading = true
    //   updateUserStatus(row.id, status).then(() => {
    //     this.listLoading = false
    //     row.userStatus = status
    //     this.$message({
    //       message: '状态修改成功',
    //       type: 'success'
    //     })
    //   })
    // },
    handleDownload() {
      this.downloadLoading = true;
      fetchExportList(this.listQuery).then(response => {
        const exportList = response.data;
        import("@/vendor/Export2Excel").then(excel => {
          const tHeader = [];
          const filterVal = this.formThead
          for (const item of filterVal) {
            tHeader.push(labelMap[item])
          }
          const data = this.formatJson(filterVal, exportList);
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: "待跟进线索列表"
          });
          this.downloadLoading = false;
        });
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === "createTime") {
            return parseTime(v[j]);
          } else if (j === "updateTime") {
            return parseTime(v[j]);
          } else if (j === "enrollCourseNames") {
            return v['enrollCourseList'].length + '门'
          }else if (j === "consultCourseNames") {
            return v['clueCourList'].length+ '门'
          }else if (j === "auditonCourseNames") {
            return v['auditionList'].length+ '门'
          }else {
            return v[j];
          }
        })
      );
    },
    toEdit(id) {
      this.$router.push({
        path: "/business/editClue",
        query: {
          id: id
        }
      });
    },
    //所有的获取下拉框数据方法
    //获取所有校区()
    getOrgIdOptions() {
      const queryMethod = "queryClueOrStatistic";
      fetchOrg(queryMethod).then(response => {
        const orgArr = response.data;
        const publicOrg = {
          children: null,
          label: "公共校区",
          value: "0"
        };
        const index = orgArr.indexOf(publicOrg);
        if (index !== -1) {
          orgArr.splice(index, 1);
        }
        this.orgIdOptions = orgArr;
        debugger;
      });
    },
    //根据校区获取所有渠道类型(校区ID)
    getChannelTypeOptions(queryId, queryKey) {
      fetchChannel(queryId, "0").then(response => {
        this.channelTypeIdOptions = response.data;
      });
    },
    //根据渠道ID获取对应的渠道(校区ID)
    getChannelOptions(queryId, queryKey) {
      fetchChannel(queryId, "1").then(response => {
        this.channelIdOptions = response.data;
      });
    },
    //科系（校区Id）
    getDepartmentIdOptions(queryId, queryKey) {
      fetchCourse(queryId, "0").then(response => {
        this.departmentIdOptions = response.data;
      });
    },
    //课程（校区Id）
    getCourseOptions(queryId, queryKey) {
      fetchCourse(queryId, "1").then(response => {
        this.courseOptions = response.data;
      });
    },
    //咨询师(校区ID)
    getOwnerOptions(orgId) {
      fetchOwner(orgId).then(response => {
        this.ownerIdOptions = response.data;
      });
    },

    //所有change事件
    //校区变化（重置渠道类型、渠道、科系、课程、咨询师）
    handleOrgIdChange(orgId) {
      this.listQuery.channelTypeId = "";
      this.listQuery.channelId = "";
      this.listQuery.departmentId = "";
      this.listQuery.courseId = "";
      this.listQuery.ownerId = "";

      this.getChannelTypeOptions(orgId);
      this.getDepartmentIdOptions(orgId);
      this.getOwnerOptions(orgId);

      this.channelIdOptions = null;
      this.courseOptions = null;
    },
    //渠道类型变化（重置渠道）
    handleChannelTypeIdChange(channelTypeId) {
      this.listQuery.channelId = "";
      this.getChannelOptions(channelTypeId);
    },
    //科系变化（重置课程）
    handleDepartmentIdChange(departmentId) {
      this.listQuery.courseId = "";
      this.getCourseOptions(departmentId);
    },

    //咨询日期变化（设置开始日期和结束日期）
    handleCreateTimeChange(t) {
      // console.log(this.createTime)
      // this.listQuery.createTimeStart = this.createTime[0];
      // this.listQuery.createTimeEnd = this.createTime[1];
      // console.log(this.listQuery.createTimeStart);
      // console.log(this.listQuery.createTimeEnd);
      //console.log(this.listQuery.shop); //结果为一个数组，如：["2018-08-04", "2018-08-06"]
    },
    //诺到访日期变化（设置开始日期和结束日期）
    handlePromiseVisitDateChange(t) {
      // this.listQuery.promiseVisitDateStart = this.promiseVisitDate[0];
      // this.listQuery.promiseVisitDateEnd = this.promiseVisitDate[1];
      // console.log(this.listQuery.promiseVisitDateStart);
      // console.log(this.listQuery.promiseVisitDateEnd);
      //console.log(this.listQuery.shop); //结果为一个数组，如：["2018-08-04", "2018-08-06"]
    },
    //到访日期变化（设置开始日期和结束日期）
    handleVisitTimeChange(t) {
      // this.listQuery.visitTimeStart = this.visitTime[0];
      // this.listQuery.visitTimeEnd = this.visitTime[1];
      // console.log(this.listQuery.visitTimeStart);
      // console.log(this.listQuery.visitTimeEnd);
      //console.log(this.listQuery.shop); //结果为一个数组，如：["2018-08-04", "2018-08-06"]
    },
    toggleMoreFilters() {
      if (this.displayMoreFilters === "display:none") {
        this.displayMoreFilters = "";
        this.toggleBtnText = "收起";
      } else {
        this.displayMoreFilters = "display:none";
        this.toggleBtnText = "展开";
      }
    }
  }
};
</script>
<style scoped>
.pd-10 {
  margin-bottom: 14px;
}
</style>
